﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TVNNewDBLayer.DAL
{
    class ArticleDAL:ICommon<TB_Article, int>
    {
        TVNNewsDataContext db;
        public ArticleDAL()
        {
            db = new TVNNewsDataContext();
        }

        public ArticleDAL(TVNNewsDataContext db)
        {
            this.db = db;
        }

        public List<TB_Article> GetAll()
        {
            return db.TB_Articles.ToList();
        }

        public TB_Article Get(int p)
        {
            return db.TB_Articles.SingleOrDefault(a => a.ArticleID == p);
        }

        public TB_Article Insert(TB_Article p)
        {
            try
            {
                db.TB_Articles.InsertOnSubmit(p);
                db.SubmitChanges();
                return p;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public bool Update(TB_Article p)
        {
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int p)
        {
            try
            {
                TB_Article a = Get(p);
                db.TB_Articles.DeleteOnSubmit(a);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
